﻿;(function($){
    $.extend($.fn,{
        validate:function(options){
            var currentform=this.parents('form');
            var validatedefault=$.extend({
                events:{blur:true},
                rules:{},
                messages:{}
            },options);//var validatedefault
            $.each(validatedefault.events,function(event,value){
                $.each(validatedefault.rules,function(targetid,options){
                    var key=targetid+event;
                    var valid= $.data(currentform,key);
                    if(valid){
                        return;
                    }
                    var target=$("#"+targetid); 
                    if(value){
                        var targetmsgs=validatedefault.messages[targetid];
                        target.bind(event,{obj:target,setting:options,msg:targetmsgs},function(e){
                            var noerror=$.validator(e.data.obj,e.data.setting,e.data.msg);
                            $.data(currentform,targetid,noerror);
                            var abc;
                        });//target.bind(event
                    }//if(value);
                })//$.each(validatedefault.rules
            });// $.each(validatedefault.event
            $('input[type=submit]',currentform).click(function(e){
                var isvalidate=true;                       
                if($(e.target).hasClass('cancel')){
                    return isvalidate;
                }else{
                    var haserror=undefined;
                    $.each(validatedefault.rules,function(targetid,options){
                        var obj=$.data(currentform,targetid);
                        if(obj==undefined){
                            $('#'+targetid+':enabled').change();
                        }
                        haserror=haserror&&obj;
                    });//$.each;
                    isvalidate=haserror;
                    return (isvalidate==undefined)?false:isvalidate;
                }
            });
        }//Validate:function;
    });// $.extend($.fn,
  
    $.validator=function(target,setting,msg){
        var result=true;
        $.each(setting,function(item,value){
            if($.validator.methods[item]!=null){
                var noerror=$.validator.methods[item](target,value,msg[item]);
                result=(result==true)?noerror:false;
            }
        })//$.each(options,function(item,value)
        return result;
    }// $.validator=function(target,setting,msg)
    
    $.validator.methods=$.extend({
        showerror:function(target,message){
            var parent=target.parent();
            var existedmsg=$(':contains('+message+')',parent);
                if(existedmsg.length!=0){
                    return false;
                };
            var errormsg=$('<br/><span style="color:red">'+message+'</span>');
            parent.append(errormsg);
            return false;
        },
        removeerror:function(target,message){
            var parent=target.parent();
            var errorspan=$('span:contains('+message+')',parent);
            errorspan.prev('br').remove();
            errorspan.remove();
            return true;
        }
        //showerror;
        },{    
        required:function(target,setvalue,message){
            if(setvalue==false){
                return;
            };//if(value==false)
            var parent=target.parent();
            if(target.is('input')||target.is('select')){
                if(target.val()==''){
                    return $.validator.methods.showerror(target,message); 
                }else{
                    return $.validator.methods.removeerror(target,message);
                }//if(target.val()=='')
            }else if(target.is('radio')){
                alert('under development');
            }//if;
        },//required;
        maxlength:function(target,setvalue,message){
            var parent=target.parent();
            var length=target.val().length;
            if(length>=setvalue){
                return $.validator.methods.showerror(target,message); 
            }else{
                return $.validator.methods.removeerror(target,message);
            }//if(length=0);
        },//maxlength;
        minlength:function(target,setvalue,message){
            var parent=target.parent();
            var length=target.val().length;
            if(length<setvalue){
                return $.validator.methods.showerror(target,message); 
            }else{
                return $.validator.methods.removeerror(target,message);
            }//if(length=0);
        },//maxlength;
        email:function(target,setvalue,message){
            if(setvalue!=true){
                return;
            };
            var emailpattern= /^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/; 
            if(!emailpattern.test(target.val())){ 
                return $.validator.methods.showerror(target,message); 
            }else{
                return $.validator.methods.removeerror(target,message);
            }
        },
        number:function(target,setvalue,message){
            if(setvalue!=true){
                return;
            };
            var numberpattern=/^-?\d+$/;
            if(!numberpattern.test(target.val())){
                return $.validator.methods.showerror(target,message); 
            }else{
                return $.validator.methods.removeerror(target,message);
            }
            
        },
        remote:function(target,setvalue,message){
            if(typeof(setvalue)=='object'){
                var url,errorhandler,successhandler,data={};
                $.each(setvalue,function(item,value){
                    switch(item){
                        case 'url':
                            url=value;
                            break;
                        case 'success':
                            successhandler=value;
                            break;
                        case 'error':
                            errorhandler=value;
                            break;
                    }//switch
                });//$.each;
                var returnvalue;
                $.ajax({
                    async:false,
                    type:'post',
                    url:url,
                    data:data,
                    success:function(reponse){
                        var iscorrect=successhandler(reponse);
                        if(iscorrect==undefined){
                            alert('please return "true" or "false" within success function');
                            return false;
                        }
                        if(iscorrect==false){
                            returnvalue= $.validator.methods.showerror(target,message); 
                        }else{
                            returnvalue= $.validator.methods.removeerror(target,message);
                        }
                    },
                    error:function(response){
                        errorhandler(response);
                        returnvalue= $.validator.methods.showerror(target,message);
                    }
                });//$.ajax;
                return returnvalue;
            }//if(typeof(setvalue));
        }
    })// $.validator.methods
    
})(jQuery);